/*
 * Copyright 2009 Armando Blancas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package taskgraph.reflect;

import java.lang.reflect.Method;

/**
 * Stores reflection data about getter and setter methods, and their optional
 * annotation values: position and field name. The position field defaults to
 * zero; the field name defaults to the property name.
 * 
 * <p>This data is used by the I/O reflection mechanism in order to associate
 * Bean properties with field names either by position or by name. Field names
 * may be located in flat files or databases.
 * 
 * @author Armando Blancas
 */
public class Accessor implements Comparable<Accessor> {

	int position;
	String propertyName;
	Method method;

	public Accessor(int position, String propertyName, Method method) {
		setPosition(position);
		setPropertyName(propertyName);
		setMethod(method);
	}
	
	@Override
	public int compareTo(Accessor accessor) {
		/*
		 * This works because this class constructor and setPosition() 
		 * methods guarantee that position cannot be negative.
		 */
		return position - accessor.position;
	}

	/**
	 * Gets the accessor method.
	 * 
	 * @return The {@code Method} reflection object.
	 */
	public Method getMethod() {
		return method; 
	}

	/**
	 * Gets the optional position value for the property 
	 * associated to this accessor.
	 * 
	 * @return An optional position number.
	 */
	public int getPosition() { 
		return position; 
	}
		
	/**
	 * Gets the name of the property associated to this accessor.
	 * 
	 * @return The {@code String} name of the property
	 */
	public String getPropertyName() {
		return propertyName;
	}
		
	/**
	 * Set the method for this accessor.
	 * 
	 * @param method The {@code Method} object to set.
	 */
	public void setMethod(Method method) {
    	if (method == null)
    		throw new IllegalArgumentException("method == null");
		this.method = method;
	}
	
	/**
	 * Sets the position of the property associated to
	 * this accessor.
	 * 
	 * @param position The position to set.
	 */
	public void setPosition(int position) {
    	if (position < 0)
    		throw new IllegalArgumentException("position < 0");
		this.position = position;
	}
	
	/**
	 * Sets the name of the property associated to this accessor.
	 * 
	 * @param propertyName The {@code String} value to set.
	 */
	public void setPropertyName(String propertyName) {
    	if (propertyName == null)
    		throw new IllegalArgumentException("propertyName == null");
		this.propertyName = propertyName;
	}

}
